home *** CD-ROM | disk | FTP | other *** search
- unit LogOffSurvivorU;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- ListBox1: TListBox;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- public
- function ApplicationHook(var Message: TMessage): Boolean;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure RegisterMe(Register: Boolean);
- type
- TRegisterServiceProcess = function(dwProcessId, dwType: DWord): LongBool; stdcall;
- const
- RegisterServiceProcess: TRegisterServiceProcess = nil;
- RSP_SIMPLE_SERVICE = 1;
- RSP_UNREGISTER_SERVICE = 0;
- Types: array[Boolean] of DWord = (RSP_UNREGISTER_SERVICE, RSP_SIMPLE_SERVICE);
- begin
- if not Assigned(@RegisterServiceProcess) then
- RegisterServiceProcess := GetProcAddress(
- GetModuleHandle('Kernel32.dll'), 'RegisterServiceProcess');
- if Assigned(@RegisterServiceProcess) then
- Win32Check(RegisterServiceProcess(0, Types[Register]))
- // API won't be found on Windows NT/2000, but that's okay
- // else
- // raise EWin32Error.Create('RegisterServiceProcess API not located');
- // raise EWin32Error.Create('RegisterServiceProcess API not located');
- end;
-
- function TForm1.ApplicationHook(var Message: TMessage): Boolean;
- begin
- Result := False;
- //If this is a user logoff...
- if (Message.Msg = WM_ENDSESSION) and
- (TWMEndSession(Message).Unused = LParam(ENDSESSION_LOGOFF)) then
- begin
- //...Stop this message being processed by the
- //Application object, which would shut the application
- Result := True;
- Message.Result := 0;
- end
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- RegisterMe(True);
- Application.HookMainWindow(ApplicationHook)
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- Application.UnhookMainWindow(ApplicationHook);
- RegisterMe(False)
- end;
-
- end.
-